/**
 * MIT License
 * 
 * Copyright (c) 2024 - present @ ebraid
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <types.h>


/**
 * @brief kfifo structure
 * @in:    the input index
 * @out:   the output index
 * @mask:  the size mask, it is equal to size - 1
 * @size:  the size of the fifo, it must be a power of 2
 * @data:  the fifo data
 */
typedef struct kfifo {
	unsigned int	in;
	unsigned int	out;
    unsigned int	mask;
	unsigned int	size;
	void		    *data;
}kfifo_t;


/**
 * @brief  initialize a kfifo structure
 * @param  name:  the name of the kfifo structure
 * @param  size:  the size of the kfifo structure, it must be a power of 2
 */
#define  KFIFO_INIT(name, size)   \
	struct kfifo name = { \
		.in = 0, \
		.out = 0, \
		.mask = (size) - 1, \
		.size = (size), \
		.data = NULL, \
	};


/**
 * @brief  get the used size of the kfifo structure
 * @param  fifo:  the kfifo structure
 * @return the used size of the kfifo structure
 */
static inline size_t kfifo_used(const struct kfifo *fifo) {
    return fifo->in - fifo->out;
}


/**
 * @brief  get the unused size of the kfifo structure
 * @param  fifo:  the kfifo structure
 * @return the unused size of the kfifo structure
 */
static inline size_t kfifo_unused(const struct kfifo *fifo) {
    return fifo->size - kfifo_used(fifo);
}


/**
 * @brief alloc a kfifo struct
 * @param fifo  pointer to kfifo struct
 * @param size  size of fifo, must be power of 2
 * @return 0 on success, -1 on error
 */
int kfifo_alloc(struct kfifo *fifo, size_t size);


/**
 * @brief free a kfifo struct
 * @param fifo  pointer to kfifo struct
 * @return none
 */
void kfifo_free(struct kfifo *fifo);


/**
 * @brief push data to kfifo
 * @param fifo  pointer to kfifo struct
 * @param data  pointer to data
 * @param len   length of data
 * @return length of data push
 */
size_t kfifo_push(struct kfifo *fifo, const void *data, size_t len);


/**
 * @brief pop data from kfifo
 * @param fifo  pointer to kfifo struct
 * @param data  pointer to data
 * @param len   length of data
 * @return length of pop data
 */
size_t kfifo_pop(struct kfifo *fifo, void *data, size_t len);
